import {Store,Module,ActionContext} from 'vuex'
import ListModule from '../list-module'
import ListState from '../list-state'
import News from '@/store/entities/articles/News'
import NewsCategory from '@/store/entities/articles/NewsCategory'
import Ajax from '../../../lib/ajax'
import PageResult from '@/store/entities/page-result';
import ListMutations from '../list-mutations'

interface NewsState extends ListState<News>{
    editNews: News;
    allCateList: Array<NewsCategory>;
}

class NewsModule extends ListModule<NewsState,any,News> {
    state={
        totalCount:0,
        currentPage:1,
        pageSize:10,
        list: new Array<News>(),
        loading:false,
        editNews:new News(),
        allCateList: new Array<NewsCategory>()
    };
    actions = {
        async getAll(context:ActionContext<NewsState,any>,payload:any){
            context.commit("setLoading",true);
            let reponse=await Ajax.get('/api/services/app/News/GetAll',{params:payload.data});
            context.commit("setLoading",false);
            let page=reponse.data.result as PageResult<News>;
            context.commit("setTotalCount",page.totalCount)
            context.commit("setList",page.items);
        },
        async getAllCates(context:ActionContext<NewsState,any>,payload:any){
            let reponse= await Ajax.get('/api/services/app/NewsCategory/GetAllList',{params:payload.data});
            let page=reponse.data.result as Array<NewsCategory>;
            context.commit("setAllCateList",page);
        },
        async create(context:ActionContext<NewsState,any>,payload:any){
            await Ajax.post('/api/services/app/News/Create',payload.data);
        },
        async update(context:ActionContext<NewsState,any>,payload:any){
            await Ajax.put('/api/services/app/News/Update',payload.data);
        },
        async delete(context:ActionContext<NewsState,any>,payload:any){
            await Ajax.delete('/api/services/app/News/Delete?Id='+payload.data.id);
        },
        async get(context:ActionContext<NewsState,any>,payload:any){
            let reponse=await Ajax.get('/api/services/app/News/Get?Id='+payload.id);
            return reponse.data.result as News;
        }
    };
    mutations={
        setCurrentPage(state:NewsState,page:number){
            state.currentPage=page;
        },
        setPageSize(state:NewsState,pagesize:number){
            state.pageSize=pagesize;
        },
        setTotalCount(state:NewsState,totalCount:number) {
            state.totalCount = totalCount;
        },
        setList(state:NewsState,list: Array<News>) {
            state.list = list;
        },
        setLoading(state:NewsState,loading:boolean) {
            state.loading = loading;
        },
        edit(state:NewsState,News:News){
            state.editNews=News;
        },
        setAllCateList(state:NewsState, cate:Array<NewsCategory>){
            state.allCateList = cate;
        }
    }
}

const newsModule = new NewsModule();
export default newsModule;